<?php
/**
 *
 * 版权所有：安德玛(underArmour)
 * 作    者：1554324405@qq.com
 * 日    期：2020-05-20
 * 版    本：1.0.0
 * 功能说明：会员动态控制器。
 *
 **/

namespace Admin\Controller;

class DynamicController extends ComController
{
    /**
     * 列表
     */
    public function index()
    {
        $key = I('get.keyword','','htmlspecialchars');
        $p = isset($_GET['p']) ? intval($_GET['p']) : '1';
        $m = M('user_dynamic');
        
        if($key){
            $where['mobile'] = array('like','%'.$key.'%');
        }
        $pagesize =20;#每页数量
        $offset = $pagesize * ($p - 1);//计算记录偏移量
        $count = $m->where($where)->count();
        $list = $m->order('t desc')->where($where)->limit($offset . ',' . $pagesize)->select();
        $page = new \Think\Page($count, $pagesize);
        $page = $page->show();
        $this->assign('list', $list);
        $this->assign('page', $page);
        $this->assign('p',$p);
        $this->display();  
    }
    
    
    /**
     * 新增
     */
    public function add()
    {
        if(IS_POST)
        {
            $data['mobile'] = I('post.mobile','','htmlspecialchars');
            $data['content'] = I('post.content','','htmlspecialchars');
            $data['t'] = I('post.t','','htmlspecialchars');
            if(!$data['mobile'] or !$data['content'] or !$data['t']){
                $this->error('表单项存在空项！');
            }
            $data['t'] = strtotime($data['t']);
            $res = M('user_dynamic')->data($data)->add();
            if($res){
                addlog('新增会员动态，ID：'.$res);
                $this->success('操作成功',U('index'));
            }else{
                $this->error('操作失败，请稍后再试！');
            }
        }
        else
        {
           $this->assign('method',U('Dynamic/add'));
           $this->assign('nowtime',date('Y-m-d H:i:s',time()));
           $this->display('form');
        }
    }
    
    
    /**
     * 修改
     */
    public function update()
    {
        $id = I('id');
        $p = I('p',1);
        if(!$id){
            $this->error('传参为空！');
        }
        if(IS_POST){
            $data['mobile'] = I('post.mobile','','htmlspecialchars');
            $data['content'] = I('post.content','','htmlspecialchars');
            $data['t'] = I('post.t','','htmlspecialchars');
            if(!$data['mobile'] or !$data['content'] or !$data['t']){
                $this->error('表单项存在空项！');
            }
            $data['t'] = strtotime($data['t']);
            $data['update_t'] = time();
            $res = M('user_dynamic')->data($data)->where('id='.$id)->save();
            if($res){
                addlog('修改会员动态，ID：'.$id);
                $this->success('操作成功',U('index',array('p'=>$p)));
            }else{
                $this->error('操作失败，请稍后再试！');
            }
        }else{
            $vo = M('user_dynamic')->where('id='.$id)->find();
            if(!$vo){
                $this->error('记录不存在！');
            }
            $this->assign('method',U('Dynamic/update',array('p'=>$p)));
            $this->assign('vo',$vo);
            $this->assign('p',$p);
            $this->display('form');
        }
    }
    
    
    /**
     * 删除
     */
    public function delete()
    {
        $ids = isset($_REQUEST['ids']) ? $_REQUEST['ids'] : false;
        $p = isset($_GET['p']) ? intval($_GET['p']) : '1';
        if ($ids) {
            if (is_array($ids)) {
                $ids = implode(',', $ids);
                $map['id'] = array('in', $ids);
            } else {
                $map = 'id=' . $ids;
            }
            if (M('user_dynamic')->where($map)->delete()) {
                addlog('删除会员动态，ID：' . $ids);
                $this->success('恭喜，删除会员动态成功！',U('index',array('p'=>$p)));
            } else {
                $this->error('参数错误！');
            }
        } else {
            $this->error('参数错误！');
        }
    }
    
}